﻿
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BoAoCoffee.Model
{
    public class IceMakingMsg
    {
        private static IceMakingMsg instance;
        private IceMakingMsg() { }

        public static IceMakingMsg Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new IceMakingMsg();
                }
                return instance;
            }
        }
        #region IO通讯
        public bool AllBol = true;
        public bool RobotBol = true;
        public delegate void IOdelegate(string btnName, string tag);
        public IOdelegate iodelegate;
        /// <summary>
        ///  IO的通讯返回数据
        /// </summary>
        private List<byte> buffer = new List<byte>();
        public static bool IsMsgError = true;
        string nostrs = "";
        public Thread All_thr;
        public void IOSerial_Thr()
        {
            try
            {
                var ddd = SerialPort.GetPortNames().ToList();
                if (!SerialPort.GetPortNames().ToList().Any(a => a.ToLower().Contains(IPorPortInfo.Instance.ZBPortName)))
                {
                    Thread.Sleep(1000);
                    IOSerial_Thr();
                }
                IceMakingSerialPort.Instance.IceMachineConnect(IPorPortInfo.Instance.ZBPortName, 9600, "", 8, 1);//其他串口通讯
                IceMakingSerialPort.Instance.serialPort.DataReceived += SerialPort_DataReceived;
            }
            catch (Exception)
            {
                IceMakingSerialPort.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                Thread.Sleep(100);
                IOSerial_Thr();
            }
        }
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] result = new byte[IceMakingSerialPort.Instance.serialPort.BytesToRead];
                IceMakingSerialPort.Instance.serialPort.Read(result, 0, IceMakingSerialPort.Instance.serialPort.BytesToRead);
                buffer.AddRange(result);
                string str = "";// $"{DateTime.Now}：\n";
                                //判断读写模式
                if (buffer.Count > 0x03)
                    str += $"{Encoding.ASCII.GetString(result)}";
                for (int i = 0; i < result.Length; i++)
                {
                    str += $"{result[i].ToString("X2")} ";
                }
                //Console.WriteLine(str);
                MsgShow(str);
                Thread.Sleep(1);
            }
            catch (Exception)
            {
                return;
            }
        }
        string zblist = "";
        public void MsgShow(string str)
        {
            try
            {
                var note = str.Split(' ');
                string note1 = note[0].Substring(note[0].Length - 2, 2);
                if (zblist.Length == 42)
                {
                    zblist = "";
                }
                if (note1 == "FF")
                {
                    zblist = note1 + " ";
                }
                else if (zblist != "")
                    zblist += note1 + " ";
                if (zblist != "")
                {
                    for (int i = 1; i < note.Count(); i++)
                    {
                        if (note[i] != "")
                            zblist += note[i] + " ";
                    }
                }
              //  Console.WriteLine(zblist);

            }
            catch (Exception)
            {
                return;
            }
        }
        #endregion
    }
}
